package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.CarManagement;
import com.example.prison.entity.PeopleManage;
import com.example.prison.entity.VisitorAppointment;
import com.example.prison.service.CarManagementService;
import com.example.prison.service.PeopleManageService;
import com.example.prison.service.SystemLogService;
import com.example.prison.service.VisitorAppointmentService;
import com.example.prison.util.PageInfoUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "访客预约")
@RestController
@RequestMapping("/api")
public class VisitorAppointmentController {

    @Autowired
    private VisitorAppointmentService visitorAppointmentService;

    @Autowired
    private PeopleManageService peopleManageService;

    @Autowired
    private CarManagementService carManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取预约列表")
    @GetMapping("/getVisitorAppointment")
    public Response<PageInfo<VisitorAppointment>> getVisitorAppointment(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                        @Parameter(description = "访客姓名")@RequestParam(required = false) String visitorName,
                                                                        @Parameter(description = "开始时间(YYYY-MM-DD)")@RequestParam(defaultValue = "0000-00-00") String beginTime,
                                                                        @Parameter(description = "结束时间(YYYY-MM-DD)")@RequestParam(defaultValue = "9999-12-31") String endTime,
                                                                        @Parameter(description = "预约状态(1已审核/2已失效)")@RequestParam(required = false) String appointmentStatus,
                                                                        @Parameter(description = "访客状态(0未进场/1已入场/2已通过监狱门岗检查/3已提供值班人员检查/4已通过隔离仓检查/5已出场)")@RequestParam(required = false) String visitorStatus,
                                                                        String visitDepartment) {
        PageInfo<VisitorAppointment> pageInfo = visitorAppointmentService.get(pageNumber, pageSize, visitorName, beginTime, endTime, appointmentStatus, visitorStatus, visitDepartment);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增访客预约")
    @PostMapping("/addVisitorAppointment")
    public Response<?> addVisitorAppointment(@RequestBody VisitorAppointment visitor) {
        int result = visitorAppointmentService.add(visitor);
        if (result == -1) {
            return Response.error("该访客该时间已成功预约！");
        } else if (result == 0) {
            return Response.error("无此权限！");
        }
        log.info("新增访客预约"+", 数据："+ JSON.toJSONString(visitor));
        return Response.success("预约提交成功！");
    }

    @ApiOperation("删除访客预约")
    @PostMapping("/deleteVisitorAppointment")
    public Response<?> deleteVistorAppointment(@Parameter(description = "预约编号") @RequestBody Integer[] ids) {
        visitorAppointmentService.delete(ids);
        log.info("删除访客预约");
        systemLogService.add("删除访客预约");
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑访客预约")
    @PostMapping("/updateVisitorAppointment")
    public Response<?> updateVistorAppointment(@RequestBody VisitorAppointment visitor) {
        Integer id = visitor.getId();
        int result = visitorAppointmentService.update(visitor, id);
        if (result == -1) {
            return Response.error("预约已审核！");
        }
        log.info("编辑访客预约"+", 数据："+JSON.toJSONString(visitor));
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取预约")
    @GetMapping("/getVisitorAppointmentById")
    public Response<VisitorAppointment> getVistorAppointmentById(@Parameter(description = "编号(id)")@RequestParam Integer id) {
        VisitorAppointment vistor = visitorAppointmentService.getById(id);
        if(ObjectUtils.isEmpty(vistor)) {
            return Response.error("此预约编号不存在！");
        }
        return Response.success("获取成功！", vistor);
    }

    @ApiOperation("依据预约状态获取预约车辆数量")
    @GetMapping("/getVisitorAppointmentCarNumberByStatus")
    public Response<?> getCarNumberByStatus(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                            @Parameter(description = "预约状态(1已审核/2已失效)")@RequestParam String appointmentStatus) {
        String sum = visitorAppointmentService.getCarNumberSum(appointmentStatus);
        if(ObjectUtils.isEmpty(sum)) {
            sum = "0";
        }
        return Response.success("获取成功！该状态下预约车辆数量："+sum, sum);
    }

    @ApiOperation("获取今日访客列表")
    @GetMapping("/getTodayVisitor")
    public Response<PageInfo<VisitorAppointment>> getTodayVisitor(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                  @Parameter(description = "访客姓名")String visitorName) {
        PageInfo<VisitorAppointment> list = visitorAppointmentService.getTodayVisitor(pageNumber, pageSize,visitorName);
        return Response.success("获取成功！", list);
    }

    @ApiOperation("获取今日来访车辆")
    @GetMapping("/getTodayVisitorCar")
    public Response<?> getTodayVisitorCar(@Parameter(description = "车牌号")String carLicense) {
        String sum = visitorAppointmentService.getTodayVisitorCarSum();
        if(ObjectUtils.isEmpty(sum)) {
            sum = "0";
        }
        PageInfo<VisitorAppointment> list = visitorAppointmentService.getTodayVisitorCar(carLicense);
        return Response.success("获取成功！今日来访车辆数量："+sum, list);
    }


    @ApiOperation("获取滞留访客")
    @GetMapping("/getRetentionVisitor")
    public Response<?> getRetentionVisitor(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                           @Parameter(description = "访客姓名")String visitorName,
                                           @Parameter(description = "开始时间(YYYY-MM-DD)")String beginTime,
                                           @Parameter(description = "结束时间(YYYY-MM-DD)")String endTime) {
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        PageInfo<VisitorAppointment> list = visitorAppointmentService.getRetentionVisitor(pageNumber,pageSize,"5", time, visitorName, beginTime, endTime);
        return Response.success("获取成功！", list);
    }

    @ApiOperation("获取滞留车辆")
    @GetMapping("/getRetentionCar")
    public Response<?> getRetentionCar(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                       @Parameter(description = "车牌号")String carLicense) {
        PageInfo<CarManagement> list = carManagementService.getRetentionCar(pageNumber,pageSize, carLicense, "");
        return Response.success("获取成功！", list);
    }

    @ApiOperation("删除滞留车辆")
    @GetMapping("/deleteRetentionCar")
    public Response<?> deleteRetentionCar(@Parameter(description = "车牌号",required = true)String carLicense) {
        CarManagement car = carManagementService.getOneByNumber(carLicense);
        Integer[] ids = {car.getCarId()};
        carManagementService.delete(ids);
        return Response.success("获取成功！");
    }

    @ApiOperation("指定访客接待人员")
    @GetMapping("/designatedReceptionist")
    public Response<?> designatedReceptionist(@Parameter(description = "访客id",required = true)Integer visitorId,
                                              @Parameter(description = "人员id",required = true)Integer receptionId) {
        if(ObjectUtils.isEmpty(visitorAppointmentService.getById(visitorId))) {
            return Response.error("访客id不存在！");
        }
        if(ObjectUtils.isEmpty(peopleManageService.getById(receptionId))) {
            return Response.error("人员id不存在！");
        }
        VisitorAppointment visitor = visitorAppointmentService.getById(visitorId);
        PeopleManage receptionist = peopleManageService.getById(receptionId);
        visitor.setReceptionist(receptionist.getName());
        visitorAppointmentService.update(visitor,visitorId);
        return Response.success("指定成功！");
    }

}

